//
//  HomeViewController.m
//  News
//
//  Created by 徐涵菲 on 2018/4/23.
//  Copyright © 2018年 Wangly. All rights reserved.
//

#import "HomeViewController.h"
#import "Tool.h"
#import "ScanViewController.h"
#import "TitleButton.h"
#import "PopMenuView.h"
#import "TestTableViewController.h"

@interface HomeViewController ()

@property (nonatomic, weak) UIButton *addBtn;
@property (nonatomic, weak) UITextField *tf;

@end
UIWindow *mywindow;
@implementation HomeViewController

-(void)loadView
{
    self.view=[DefaultCenterView defaultCenterView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
#warning 测试控件
    
    UIView *redView=[[UIView alloc] init];
    redView.backgroundColor=[UIColor redColor];
    redView.frame=CGRectMake(100, 300, 200, 200);
    [self.view addSubview:redView];
    
    UIButton*addBtn=[UIButton buttonWithType:UIButtonTypeContactAdd];
    addBtn.center=CGPointMake(100, 100);
    [redView addSubview:addBtn];
    self.addBtn=addBtn;
    
    UITextField *tf=[[UITextField alloc] init];
    tf.frame=CGRectMake(0, 0, 100, 44);
    tf.borderStyle=UITextBorderStyleRoundedRect;
    [redView addSubview:tf];
    self.tf=tf;
    
    //1.初始化导航条
    //2.创建左边和右边按钮
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithImage:@"navigationbar_friendsearch" higImage:@"navigationbar_friendsearch_highlighted" target:self action:@selector(friendsearch)];
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithImage:@"navigationbar_pop" higImage:@"navigationbar_pop_highlighted" target:self action:@selector(scan)];
    //标题
    // UIButton *titleBtn=[[UIButton alloc] init];
    TitleButton *titleBtn=[[TitleButton alloc] init];
    [titleBtn setTitle:@"首页" forState:UIControlStateNormal];
    // 设置默认状态图片
    [titleBtn setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
    // 设置选中状态图片
    [titleBtn setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
    [titleBtn sizeToFit];
    
    //[titleBtn setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
    //设置颜色
    //[titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //设置frame
    [titleBtn sizeToFit];
    //监听标题按钮点击事件
    [titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.titleView = titleBtn;
    
}

//

-(UIBarButtonItem *)itemWithImage:(NSString *)image higImage:(NSString *)higImage action:(SEL)action;
{
    //创建按钮
    UIButton *btn=[[UIButton alloc]init];
    //设置对应状态图片
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:higImage] forState:UIControlStateHighlighted];
    //设置frame
    btn.size=btn.currentImage.size;
    //添加监听事件
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

#pragma mark - 内部控制方法

TestTableViewController *_testVc;

-(void)titleBtnClick:(TitleButton *)titleBtn
{
    // DDActionLog;
    //1.切换按钮状态
    titleBtn.selected=!titleBtn.selected;
    
    
     // 需要显示内容
     TestTableViewController *testVc = [[TestTableViewController alloc] init];
     testVc.view.size = CGSizeMake(150, 200);
     
     [PopMenuView popFrom:titleBtn contentVC:testVc dismiss:^{
     // 1.切换按钮的状态
     titleBtn.selected = !titleBtn.selected;
    }];
}

/*
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"abc" message:@"123" delegate:nil cancelButtonTitle:@"cancle" otherButtonTitles:nil, nil];
    [alert show];
}
*/

-(void)friendsearch
{
    DDActionLog;
}

-(void)scan
{
    DDActionLog;
    //弹出二维码扫描
    //ScanViewController *scanVc=[[ScanViewController alloc] init];
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"ScanViewController" bundle:nil];
    ScanViewController *scanVc=[sb instantiateInitialViewController];
    [self presentViewController:scanVc animated:YES completion:nil];
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"首页 - %tu", indexPath.row];
    return cell;
}


@end
